package com.ht.medicaltreatment.activity.haocai2

import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.UiMessageUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.UserInfoAdapter
import com.ht.medicaltreatment.api.ConsumableApprovalAPI
import com.ht.medicaltreatment.bean.UserInfoData
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.viewmodel.SendOffViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_send_off_layout.etAddress
import kotlinx.android.synthetic.main.activity_send_off_layout.etPhone
import kotlinx.android.synthetic.main.activity_send_off_layout.recyclerView
import kotlinx.android.synthetic.main.activity_send_off_layout.tvRFID
import kotlinx.android.synthetic.main.activity_send_off_layout.tvScan
import kotlinx.android.synthetic.main.activity_send_off_layout.tvSubmit
import kotlinx.android.synthetic.main.activity_send_off_layout.tvUserName

//寄出 信息填写
class SendOffActivity:BaseActivity(), View.OnClickListener, OnItemClickListener {

    override fun getLayoutId() = R.layout.activity_send_off_layout

    private val model by viewModels<SendOffViewModel>()
    private var id = -1
    private var userId = -1

    override fun init() {
        setTitle("耗材寄送信息")
        id = intent.getIntExtra("id",0)

        recyclerView.layoutManager = LinearLayoutManager(this)
        val adapter = UserInfoAdapter()
        recyclerView.adapter = adapter
        ScanUtils.init()
        model.getUserList {
            adapter.setNewInstance(it)
        }
        adapter.setOnItemClickListener(this)
        tvUserName.setOnClickListener(this)
        tvScan.setOnClickListener(this)
        tvSubmit.setOnClickListener(this)
        recyclerView.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v){
            tvUserName -> {
                recyclerView.visibility = View.VISIBLE
            }

            tvScan -> {
                ScanUtils.open {
                    ScanUtils.close()
                    tvRFID.text = it
                }
            }

            recyclerView -> {
                recyclerView.visibility = View.GONE
            }

            tvSubmit -> {
                val phone = etPhone.text.toString()
                val address = etAddress.text.toString()
                val rfid = tvRFID.text.toString()
                if(userId == -1 || TextUtils.isEmpty(phone)
                    || TextUtils.isEmpty(address)|| TextUtils.isEmpty(rfid)){
                    showToast("请填写完整信息")
                    return
                }

                val api = ConsumableApprovalAPI(
                    sign = 3,
                    id = id,
                    recipientId = "$userId",
                    recipientName = tvUserName.text.toString(),
                    recipientPhone = phone,
                    recipientAddress = address,
                    expressUid = rfid

                )
                showProgress()
                model.approval(api, {
                    dismissProgress()
                    UiMessageUtils.getInstance().send(121)
                    finish()
                })
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as UserInfoData
        userId = item.id
        tvUserName.text = item.name
        recyclerView.visibility = View.GONE
    }
}